//
//  MyPreferences.m
//  Personal Passlock
//
//  Created by 杨岑 on 5/16/15.
//  Copyright (c) 2015 杨岑. All rights reserved.
//

#import "MyPreferences.h"

#define kPRIMARY_DOCUMENT   @"primary doc"

static MyPreferences* _singleton = nil;

@implementation MyPreferences
{
    BOOL _initCalled;
}

#pragma mark Singleton

+ (instancetype)singleton
{
    @synchronized(self)
    {
        if (!_singleton)
        {
            _singleton = [[MyPreferences alloc] init];
        }
    }
    return _singleton;
}

+ (instancetype)instance
{
    @synchronized(self)
    {
        if (!_singleton)
        {
            _singleton = [[MyPreferences alloc] init];
        }
    }
    return _singleton;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    @synchronized(self)
    {
        if (!_singleton) _singleton = [super allocWithZone:zone];
    }
    return _singleton;
}

- (instancetype)init
{
    @synchronized(self)
    {
        if (!_initCalled)
        {
            self = [super init];
            if (self != nil)
            {
                // initialize stuff here
            }
            _initCalled = YES;
        }
    }
    
    return self;
}

#pragma mark Methods

- (NSString *)primaryDocName
{
    return [[NSUbiquitousKeyValueStore defaultStore] stringForKey:kPRIMARY_DOCUMENT];
}

- (void)setPrimaryDocName:(NSString *)primaryDoc
{
    [[NSUbiquitousKeyValueStore defaultStore] setString:primaryDoc forKey:kPRIMARY_DOCUMENT];
}

@end
